/**
 * Created by wzm on 2017/1/10.
 */
$.extend($.fn.datagrid.methods, {
    editCell: function(jq,param){
        return jq.each(function(){
            var opts = $(this).datagrid('options');//获取行数据
            var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));//返回列字段
            for(var i=0; i<fields.length; i++){
                var col = $(this).datagrid('getColumnOption', fields[i]);//遍历所有列
                col.editor1 = col.editor;
                if (fields[i] != param.field){
                    col.editor = null;
                }
            }
            $(this).datagrid('beginEdit', param.index);
            $(".textbox-text").css("position","relative");
            $(".textbox-text").after("<a class='flag' style='position:absolute;top: -5px;right: 1px'>✖</a>");
            $(".textbox-text ").focus();
            for(var i=0; i<fields.length; i++){
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor = col.editor1;
            }

        });
    }
});
var indexs = new Array() ;
var editIndex = undefined;
function endEditing(id){
  /*  if (editIndex == undefined){return true}*/
    if ($(id).datagrid('validateRow', editIndex)){//校验是否为有较行
        $(id).datagrid('endEdit', editIndex);//取消编辑事件
      /*  editIndex = undefined;*/
        return true;
    } else {
        return false;
    }
}

//去除数组中相同数据
function removeSameItem(arr) {
    var temp = {};
    for (var i = 0; i < arr.length; i++)
        temp[arr[i]] = true;
    var r = [];
    for (var k in temp)
        r.push(k);
    return r;
}

//textbox失去焦点事件
function blurAction(index,id,rowHeight) {
    $(".textbox-text").next('a').click(function () {
        $(id).datagrid('endEdit', index);//取消编辑事件
        autoHeight($(id),rowHeight);
    })
    var val_old = $(".textbox-value").val();
    $(".textbox-text ").blur(function () {
        var val_new = $(".textbox-value").val();
        if(val_old==val_new){
            //获取修改表单的行号
            indexs.pop(index);
        }else{
            indexs.push(index);
        }


    })

   /* window.ondblclick = function(){
        editIndex = undefined;
        $(id).datagrid('endEdit', editIndex);//取消编辑事件

    };*/
}

function autoHeight(obj,rowHeight) {
    var panel = obj.datagrid('getPanel');
    var tr = panel.find('div.datagrid-body tr');
    tr.each(function(){
        var td = $(this).children('td:first');
        td.css({
            "height": rowHeight+"px"
        });
    });
}